package yxy.game.pm2.command.office;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.OfficeMessage;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.office.OfficeHandle;
import yxy.game.pm2.module.office.OfficeModule;

/** 主公府 主公府界面 */
@MessageType(proto = Command.OfficeUI, classtype = OfficeMessage.OfficeUIRequest.class)
public class CMD_OfficeUI extends Command<OfficeMessage.OfficeUIRequest> {

	@Override
	public void execute(MessageSource messageSource, OfficeMessage.OfficeUIRequest request) {
		String playerCode = messageSource.getTag();

		OfficeMessage.OfficeUIResponse.Builder response = OfficeMessage.OfficeUIResponse.newBuilder();

		OfficeModule officeModule = OfficeModule.get(playerCode);
		if (officeModule == null) {
			send(messageSource, response, Result.ModuleNotOpen);// 模块未开启
			return;
		}
		OfficeHandle officeHandle = new OfficeHandle();

		response.addAllItems(officeHandle.getItems(playerCode));
		response.addAllHeros(officeHandle.heros(playerCode));

		if (officeModule.getChangeHeroCode() == null || officeModule.getTargetHeroId() == 0) {
			send(messageSource, response, Result.Success);
			return;
		}
		try {
			Hero hero = Hero.get(playerCode, officeModule.getChangeHeroCode());
			response.setChange(new HeroHandle().getHeroSimple(playerCode, hero));
			response.setTarget(officeModule.getTargetHeroId());

			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, OfficeMessage.OfficeUIResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}